package com.example.dynamictab;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.View;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.SlidingTabLayout;

public class MainActivity extends AppCompatActivity {

    SlidingTabLayout mTab;

    ViewPager mViewPager;
    FragmentPagerAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView() {
//        mTab = (SlidingTabLayout) findViewById(R.id.tab_fragment);
//        mViewPager = (ViewPager) findViewById(R.id.viewpager_fragment);

        View decorView = getWindow().getDecorView();
        mViewPager = ViewFindUtils.find(decorView, R.id.viewpager_fragment);
        /** 默认 */
        mTab = ViewFindUtils.find(decorView, R.id.tab_fragment);
        /** 默认 */
//        SlidingTabLayout tabLayout_1 = ViewFindUtils.find(decorView, R.id.tab_fragment);

        mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            // titles作为滑动标题栏，动态添加数据进去就可以了
            String[] titles = { "全部", "待付款", "待发货", "待收货", "待评价" };

            @Override
            public Fragment getItem(int position) { // 开始动态添加fragment
                FragmentTest fragment = new FragmentTest();
                Bundle bundle = new Bundle();
                bundle.putInt("position", position);
                bundle.putString("title", titles[position]);
                fragment.setArguments(bundle);
                return fragment;
            }

            @Override
            public int getCount() { // 添加fragment个数
                return titles.length;
            }

            @Override
            public CharSequence getPageTitle(int position) { // 添加fragment标题
                return titles[position];
            }
        };
        mViewPager.setAdapter(mAdapter);

        mTab.setViewPager(mViewPager);
//        mTab.setCustomTabView(R.layout.custom_tab, 0); // 添加头部xml，可自定义
//        mTab.setSelectedIndicatorColors(0xffe8303d); // 下滑线的颜色
//        mTab.setTextSelectColor(0xffe8303d);
//        mTab.setBackgroundColor(0xffffffff); // 头部背景颜色
    }
}